home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZCONTROL.C < prev    next >
C/C++ Source or Header  |  1992-02-06  |  10KB  |  376 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zcontrol.c */
  21. /* Control operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "estack.h"
  26. #include "iutil.h"
  27. #include "store.h"
  28.  
  29. /* Export the index of the 'for' operator */
  30. /* for the transfer function mapper in zcolor.c. */
  31. int i_zfor;
  32.  
  33. /* Forward references */
  34. private es_ptr find_stopped(P0());
  35.  
  36. /* exec */
  37. int
  38. zexec(register os_ptr op)
  39. {    check_op(1);
  40.     check_estack(1);
  41.     ++esp;
  42.     ref_assign(esp, op);
  43.     pop(1);
  44.     return o_push_estack;
  45. }
  46.  
  47. /* if */
  48. int
  49. zif(register os_ptr op)
  50. {    check_type(op[-1], t_boolean);
  51.     if ( op[-1].value.index )        /* true */
  52.        {    check_estack(1);
  53.         ++esp;
  54.         ref_assign(esp, op);
  55.        }
  56.     pop(2);
  57.     return o_push_estack;
  58. }
  59.  
  60. /* ifelse */
  61. int
  62. zifelse(register os_ptr op)
  63. {    check_type(op[-2], t_boolean);
  64.     check_estack(1);
  65.     ++esp;
  66.     if ( op[-2].value.index )
  67.        {    ref_assign(esp, op - 1);
  68.        }
  69.     else
  70.        {    ref_assign(esp, op);
  71.        }
  72.     pop(3);
  73.     return o_push_estack;
  74. }
  75.  
  76. /* for */
  77. private int
  78.   for_pos_int_continue(P1(os_ptr)),
  79.   i_for_pos_int_continue,
  80.   for_neg_int_continue(P1(os_ptr)),
  81.   i_for_neg_int_continue,
  82.   for_real_continue(P1(os_ptr)),
  83.   i_for_real_continue;
  84. int
  85. zfor(register os_ptr op)
  86. {    int code;
  87.     float params[3];
  88.     register es_ptr ep;
  89.     if ( r_has_type(op - 1, t_integer) &&
  90.          r_has_type(op - 2, t_integer) &&
  91.          r_has_type(op - 3, t_integer)
  92.        )
  93.         code = 7;
  94.     else if ( (code = num_params(op - 1, 3, params)) < 0 )
  95.         return code;
  96.     check_estack(7);
  97.     /* Push a mark, the control variable, the initial value, */
  98.     /* the increment, the limit, and the procedure, */
  99.     /* and invoke the continuation operator. */
  100.     mark_estack(es_for);
  101.     ep = esp += 5;
  102.     if ( (code & 3) == 3 )        /* initial & increment are ints */
  103.        {    ep[-4] = op[-3];
  104.         ep[-3] = op[-2];
  105.         if ( code == 7 )
  106.             ep[-2] = op[-1];
  107.         else
  108.             make_int(ep - 2, (long)params[2]);
  109.         if ( ep[-3].value.intval >= 0 )
  110.             make_op_estack(ep, for_pos_int_continue, i_for_pos_int_continue);
  111.         else
  112.             make_op_estack(ep, for_neg_int_continue, i_for_neg_int_continue);
  113.        }
  114.     else
  115.        {    make_real(ep - 4, params[0]);
  116.         make_real(ep - 3, params[1]);
  117.         make_real(ep - 2, params[2]);
  118.         make_op_estack(ep, for_real_continue, i_for_real_continue);
  119.        }
  120.     ep[-1] = *op;
  121.     pop(4);
  122.     return o_push_estack;
  123. }
  124. /* Continuation operators for for, separate for positive integer, */
  125. /* negative integer, and real. */
  126. /* Execution stack contains mark, control variable, increment, */
  127. /* limit, and procedure (procedure is topmost.) */
  128. /* The continuation operator is just above the top of the e-stack. */
  129. /* Continuation operator for positive integers. */
  130. private int
  131. for_pos_int_continue(register os_ptr op)
  132. {    register es_ptr ep = esp;
  133.     long var = ep[-3].value.intval;
  134.     if ( var > ep[-1].value.intval )
  135.        {    esp -= 5;    /* pop everything */
  136.         return o_pop_estack;
  137.        }
  138.     push(1);
  139.     make_int(op, var);
  140.     ep[-3].value.intval = var + ep[-2].value.intval;
  141.     ref_assign(ep + 2, ep);        /* saved proc */
  142.     esp = ep + 2;
  143.     return o_push_estack;
  144. }
  145. /* Continuation operator for negative integers. */
  146. private int
  147. for_neg_int_continue(register os_ptr op)
  148. {    register es_ptr ep = esp;
  149.     long var = ep[-3].value.intval;
  150.     if ( var < ep[-1].value.intval )
  151.        {    esp -= 5;    /* pop everything */
  152.         return o_pop_estack;
  153.        }
  154.     push(1);
  155.     make_int(op, var);
  156.     ep[-3].value.intval = var + ep[-2].value.intval;
  157.     ref_assign(ep + 2, ep);        /* saved proc */
  158.     esp = ep + 2;
  159.     return o_push_estack;
  160. }
  161. /* Continuation operator for reals. */
  162. private int
  163. for_real_continue(register os_ptr op)
  164. {    es_ptr ep = esp;
  165.     float var = ep[-3].value.realval;
  166.     float incr = ep[-2].value.realval;
  167.     if ( incr >= 0 ? (var > ep[-1].value.realval) :
  168.         (var < ep[-1].value.realval) )
  169.            {    esp -= 5;    /* pop everything */
  170.             return o_pop_estack;
  171.            }
  172.     push(1);
  173.     ref_assign(op, ep - 3);
  174.     ep[-3].value.realval = var + incr;
  175.     esp = ep + 2;
  176.     ref_assign(ep + 2, ep);        /* saved proc */
  177.     return o_push_estack;
  178. }
  179.  
  180. /* repeat */
  181. private int repeat_continue(P1(os_ptr));
  182. private int i_repeat_continue;
  183. int
  184. zrepeat(register os_ptr op)
  185. {    check_type(op[-1], t_integer);
  186.     if ( op[-1].value.intval < 0 ) return e_rangecheck;
  187.     check_estack(5);
  188.     /* Push a mark, the count, and the procedure, and invoke */
  189.     /* the continuation operator. */
  190.     mark_estack(es_for);
  191.     *++esp = op[-1];
  192.     *++esp = *op;
  193.     pop(2);
  194.     return repeat_continue(op - 2);
  195. }
  196. /* Continuation operator for repeat */
  197. private int
  198. repeat_continue(register os_ptr op)
  199. {    es_ptr ep = esp;        /* saved proc */
  200.     if ( --(ep[-1].value.intval) >= 0 )    /* continue */
  201.        {    push_op_estack(repeat_continue, i_repeat_continue);    /* push continuation */
  202.         ++esp;
  203.         ref_assign(esp, ep);
  204.         return o_push_estack;
  205.        }
  206.     else                /* done */
  207.        {    esp -= 3;        /* pop mark, count, proc */
  208.         return o_pop_estack;
  209.        }
  210. }
  211.  
  212. /* loop */
  213. private int loop_continue(P1(os_ptr));
  214. private int i_loop_continue;
  215. int
  216. zloop(register os_ptr op)
  217. {    check_op(1);
  218.     check_estack(4);
  219.     /* Push a mark and the procedure, and invoke */
  220.     /* the continuation operator. */
  221.     mark_estack(es_for);
  222.     *++esp = *op;
  223.     pop(1);
  224.     return loop_continue(op - 1);
  225. }
  226. /* Continuation operator for loop */
  227. private int
  228. loop_continue(register os_ptr op)
  229. {    register es_ptr ep = esp;        /* saved proc */
  230.     make_op_estack(ep + 1, loop_continue, i_loop_continue);    /* push continuation */
  231.     ref_assign(ep + 2, ep);
  232.     esp = ep + 2;
  233.     return o_push_estack;
  234. }
  235.  
  236. /* exit */
  237. int
  238. zexit(register os_ptr op)
  239. {    es_ptr ep = esp;
  240.     while ( ep >= esbot )
  241.        {    if ( r_has_type(ep, t_null) )    /* control mark */
  242.             switch ( (ep--)->value.index )
  243.                {
  244.             case es_for: esp = ep; return o_pop_estack;
  245.             case es_stopped: return e_invalidexit;    /* not a loop */
  246.                }
  247.         else
  248.             ep--;
  249.        }
  250.     /* Return e_invalidexit if there is no mark at all. */
  251.     /* This is different from PostScript, which aborts. */
  252.     /* It shouldn't matter in practice. */
  253.     return e_invalidexit;
  254. }
  255.  
  256. /* stop */
  257. int
  258. zstop(register os_ptr op)
  259. {    es_ptr ep = find_stopped();
  260.     if ( ep )
  261.        {    esp = ep - 1;
  262.         push(1);
  263.         make_bool(op, 1);
  264.         return o_pop_estack;
  265.        }
  266.     /* Return e_invalidexit if there is no mark at all. */
  267.     /* This is different from PostScript, which aborts. */
  268.     /* It shouldn't matter in practice. */
  269.     return e_invalidexit;
  270. }
  271.  
  272. /* stopped */
  273. int
  274. zstopped(register os_ptr op)
  275. {    check_op(1);
  276.     /* Mark the execution stack, and push a false in case */
  277.     /* control returns normally. */
  278.     check_estack(3);
  279.     mark_estack(es_stopped);
  280.     ++esp; make_false(esp);
  281.     *++esp = *op;            /* execute the operand */
  282.     pop(1);
  283.     return o_push_estack;
  284. }
  285.  
  286. /* .instopped */
  287. int
  288. zinstopped(register os_ptr op)
  289. {    push(1);
  290.     make_bool(op, find_stopped() != 0);
  291.     return 0;
  292. }
  293.  
  294. /* countexecstack */
  295. int
  296. zcountexecstack(register os_ptr op)
  297. {    push(1);
  298.     make_int(op, esp - esbot + 1);
  299.     return 0;
  300. }
  301.  
  302. /* execstack */
  303. private int execstack_continue(P1(os_ptr));
  304. private int i_execstack_continue;
  305. int
  306. zexecstack(register os_ptr op)
  307. {    /* We can't do this directly, because the interpreter */
  308.     /* might have cached some state.  To force the interpreter */
  309.     /* to update the stored state, we push a continuation on */
  310.     /* the exec stack; the continuation is executed immediately, */
  311.     /* and does the actual transfer. */
  312.     int depth = esp - esbot + 1;
  313.     check_write_type(*op, t_array);
  314.     if ( depth > r_size(op) ) return e_rangecheck;
  315.     check_estack(1);
  316.     r_set_size(op, depth);
  317.     push_op_estack(execstack_continue, i_execstack_continue);
  318.     return o_push_estack;
  319. }
  320. /* Continuation operator to do the actual transfer */
  321. private int
  322. execstack_continue(register os_ptr op)
  323. {    int depth = r_size(op);        /* was set above */
  324.     refcpy_to_old(op->value.refs, esbot, depth, "execstack");
  325.     return 0;
  326. }
  327.  
  328. /* .quit */
  329. int
  330. zquit(register os_ptr op)
  331. {    check_type(*op, t_integer);
  332.     gs_exit((int)op->value.intval);
  333.     /* gs_exit doesn't return, but just in case a miracle happens.... */
  334.     pop(1);
  335.     return 0;
  336. }
  337.  
  338. /* ------ Initialization procedure ------ */
  339.  
  340. op_def zcontrol_op_defs[] = {
  341.     {"0countexecstack", zcountexecstack},
  342.     {"1exec", zexec},
  343.     {"0execstack", zexecstack},
  344.     {"0exit", zexit},
  345.     {"2if", zif},
  346.     {"3ifelse", zifelse},
  347.     {"0.instopped", zinstopped},
  348.     {"4for", zfor, &i_zfor},
  349.     {"1loop", zloop},
  350.     {"1.quit", zquit},
  351.     {"2repeat", zrepeat},
  352.     {"0stop", zstop},
  353.     {"1stopped", zstopped},
  354.         /* Internal operators */
  355.     {"0%execstack_continue", execstack_continue, &i_execstack_continue},
  356.     {"0%for_pos_int_continue", for_pos_int_continue, &i_for_pos_int_continue},
  357.     {"0%for_neg_int_continue", for_neg_int_continue, &i_for_neg_int_continue},
  358.     {"0%for_real_continue", for_real_continue, &i_for_real_continue},
  359.     {"0%loop_continue", loop_continue, &i_loop_continue},
  360.     {"0%repeat_continue", repeat_continue, &i_repeat_continue},
  361.     op_def_end(0)
  362. };
  363.  
  364. /* Internal routines */
  365.  
  366. /* Find a `stopped' mark on the e-stack. */
  367. /* Return the e-stack pointer or 0. */
  368. private es_ptr
  369. find_stopped()
  370. {    register es_ptr ep;
  371.     for ( ep = esp; ep >= esbot; --ep )
  372.       if ( r_has_type(ep, t_null) && ep->value.index == es_stopped )
  373.         return ep;
  374.     return 0;
  375. }
  376.